// Tool base class
var WDC_activeTool;
var path;
var movePath = false;
var selectedObject = null;
var hitResult;
var hitOptions;
var WDC_Tools = [];
var WDC_Tool = Base.extend({
  init: function(){
	  this._super( false );
	  path = new Path();
      initialize();
  },
  initialize: function(){  
  },
  onMouseUp: function(event){
  },
  onMouseDrag: function(event){
  },
  onMouseMove: function(event){
  },
  onMouseDown: function(event){
  },
  color: function(){
	path = new Path();
	path.strokeColor = WDC_Color1;
  }
});
//Changer Tool
var WDC_ToolHandlerObject = Base.extend({
	change: function(module){
		path.fullySelected = false;
		WDC_activeTool = new WDC_Tools[module];
	},
	color: function(){
		if(arguments.length == 1){
			WDC_Color1 = arguments[0];
		}
		else{	
			WDC_Color1 = new HsbColor(arguments[0],arguments[1],arguments[2]);
		}
		WDC_activeTool.color();	
	}
});
WDC_ToolHandler = new WDC_ToolHandlerObject();
	
// -----

// Brush Tool
WDC_Tools['brush'] = WDC_Tool.extend({
	initialize: function(){
		tool.minDistance = 10;
        tool.maxDistance = 10;
	},
	onMouseUp: function(event){
		path.simplify();
	},
	onMouseDrag: function(event){
		path.add(event.point);
	},
	onMouseDown: function(event){
		path = new Path();
		path.strokeColor = WDC_Color1;
		path.strokeWidth = 10;
	}
});
// ----

// Special Line Tool
WDC_Tools['special_line'] = WDC_Tool.extend({
	initialize: function(){		
        tool.minDistance = 10;
	},
	onMouseDrag: function(event){
		path.add(event.point);	
		var step = event.delta;
		step.angle += 90;
		var top = event.middlePoint + step;
		var bottom = event.middlePoint - step;
		path = new Path();
		path.strokeColor = WDC_Color1;
		path.add(top);
		path.add(bottom);
	},
	onMouseDown: function(event){
	    path = new Path();
	    path.add(event.point);
	}
});
// ----

// Special Line Tool
WDC_Tools['line'] = WDC_Tool.extend({
	onMouseDrag: function(event){
		path.add(event.point);	
	},
	onMouseDown: function(event){
	   // If we produced a path before, deselect it:
		if (path) {
			path.selected = false;
		}
	
		// Create a new path and set its stroke color to black:
		path = new Path();
		path.strokeColor = WDC_Color1;
		path.add(event.point);
	
		// Select the path, so we can see its segment points:
	//	path.fullySelected = true;
	},
	onMouseUp: function(event){
		path.simplify(10);
	}
});
// ----

// Hand tool
WDC_Tools['hand'] = WDC_Tool.extend({
	initialize: function(){		
	    movePath = true;
        hitOptions = {
            segments: true,
            stroke: true,
            fill: true,
            tolerance: 5
        };
	},
	onMouseDrag: function(event){
		if (segment && event.modifiers.shift) {
			segment.point = event.point;
			path.smooth();
		} else if (this.movePath) {
			path.position += event.delta;
		}
	},
	onMouseDown: function(event){
		segment = path = null;
	
		hitResult = project.hitTest(event.point, this.hitOptions);
		project.activeLayer.selected = true;
		
	
		if (hitResult && hitResult.item) {
			if (this.selectedObject) {
				this.selectedObject.selected = false;
			}
			hitResult.item.selected = true;
			hitResult.item.fullySelected = true;
			this.selectedObject = hitResult.item;
		} else {
			this.selectedObject = null;
		}
	
		if (this.selectedObject) {
			if (event.modifiers.control) {
				if (hitResult.type == 'segment') {
					hitResult.segment.remove();
				};
				return;
			}
	
			if (hitResult) {
				path = hitResult.item;
				if (hitResult.type == 'segment') {
					segment = hitResult.segment;
				} else if (hitResult.type == 'stroke' && event.modifiers.shift) {
					var location = hitResult.location;
					segment = path.insert(location.index + 1, event.point);
					path.smooth();
				}
			}
			this.movePath = hitResult.type == 'fill';
			if (this.movePath) project.activeLayer.addChild(hitResult.item);
		}
	},

});
// -----
WDC_activeTool = new WDC_Tools['line']();
WDC_ToolHandler.color("black");
function onMouseUp(event) {
	WDC_activeTool.onMouseUp(event);
	project.layers[project.layers.length]=project.activeLayer.clone();
	project.activeLayer.remove();
	project.activeLayer = new Layer();
}
function onMouseDrag(event) {
	WDC_activeTool.onMouseDrag(event);
}
function onMouseDown(event) {
	WDC_activeTool.onMouseDown(event);
}

function onMouseMove(event) {
	WDC_activeTool.onMouseMove(event);
}
